<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="参数名称" prop="configName">
        <el-input
          v-model="queryParams.keyWord"
          placeholder="请输入参数名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['produce:brand:add']"
        >新增</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="brandList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          {{scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column label="品牌名称" align="center" prop="brandName" :show-overflow-tooltip="true" />
      <el-table-column label="首字母" align="center" prop="firstChar" :show-overflow-tooltip="true" />
      <el-table-column label="排序" align="center" prop="brandRank" :show-overflow-tooltip="true" />
      <el-table-column label="是否显示" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.reveal"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
            @change="negationReveal(scope.row.id)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否制造商" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.manufacturer"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
            @change="negationManufacturer(scope.row.id)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['produce:brand:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['produce:brand:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="品牌名称" >
          <el-input v-model="form.brandName" placeholder="请输入参数名称" />
        </el-form-item>
        <el-form-item label="品牌首字母" >
          <el-input v-model="form.firstChar" placeholder="请输入参数键名" />
        </el-form-item>
        <el-form-item label="品牌(LOGO)" >
          <el-upload
            class="upload-demo"
            action="http://localhost/dev-api/file/upload"
            :on-success="handleSuccess1"
            >
            <el-image
              style="width: 100px; height: 100px"
              v-show="form.brandLogo != null"
              :src="form.brandLogo"
            ></el-image>
            <el-button size="small" type="primary">点击上传</el-button>

            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>

          </el-upload>
        </el-form-item>
        <el-form-item label="品牌专区大图" >


          <el-upload
            class="upload-demo"
            action="http://localhost:80/dev-api/produce/brand/upload"
            :on-success="handleSuccess2"
             >
            <el-image
              style="width: 100px; height: 100px"
              v-show="form.brandImage != null"
              :src="form.brandImage"
            ></el-image>
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>


        </el-form-item>
        <el-form-item label="品牌故事" >
          <el-input v-model="form.brandHistory" placeholder="请输入参数键值" />
        </el-form-item>
        <el-form-item label="排序" >
          <el-input v-model="form.brandRank" placeholder="请输入参数键值" />
        </el-form-item>
        <el-form-item label="是否显示" >
          <el-radio-group v-model="form.reveal">
            <el-radio :label="true" >是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="制造商" >
          <el-radio-group v-model="form.manufacturer">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBrand,addBrand,updateBrand,delBrand ,getBrand,changeReveal,changeManufacturer} from "@/api/produce/brand";

export default {
  name: "Brand",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      brandList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        brandName: [
          { required: true, message: "参数名称不能为空", trigger: "blur" }
        ],

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    /**转换是否显示状态*/
    negationReveal(id){
      changeReveal(id);
      this.getList()
    },

    /**转换是否是供应商状态*/
    negationManufacturer(id){
      changeManufacturer(id);
      this.getList()
    },

    /**上传Logo*/
    handleSuccess1(response) {
      this.form.brandLogo=response.data.url;
    },

    /**上传大图*/
    handleSuccess2(response) {
      this.form.brandImage=response.data.url;
    },

    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listBrand(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.brandList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参数";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const brandId = row.id || this.ids;
      getBrand(brandId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改参数";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateBrand(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBrand(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const brandId = row.id || this.ids;
      this.$modal.confirm('是否确认删除参数编号为"' + brandId + '"的数据项？').then(function() {
          return delBrand(brandId);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
  }
};
</script>
